﻿using System;
using System.Data;

using DAL;
using Model;

namespace BLL
{
    public class RptBLL
    {
        public float GetDimWeightInActivity(int dimID,int actID)
        {
            return RptDAL.GetDimWeightInActivity(dimID, actID);
        }

        public bool IsTrust(string GUID)
        {
            return RptDAL.IsTrust(GUID);
        }

        public double GetTotalSocre(string GUID)
        {
            try
            {
                DataTable dt = GetDimensionScore(GUID);
                double sumScore = 0.0f;
                GuidBLL guidBLL = new GuidBLL();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int dimID = Int32.Parse(dt.Rows[i][0].ToString());
                    int actID = guidBLL.GetActivityId(GUID);

                    float w = GetDimWeightInActivity(dimID, actID);
                    double s = GetSingleScore(dimID, Int32.Parse(dt.Rows[i][2].ToString()));

                    sumScore = sumScore + (w * s);
                }
                return sumScore;
            }
            catch 
            {
                return 0.0f;
            }
        }

        public DataTable GetDimensionScore(string GUID)
        {
            return RptDAL.GetDimensionScore(GUID);
        }

        public DataTable GetTesterInfo(string GUID)
        {
            return RptDAL.GetTesterInfo(GUID);
        }

        public double GetRefTotalTimeMinute(string GUID)
        {
            if (string.IsNullOrEmpty(GUID))
            {
                return 0;
            }

            DataTable dt = RptDAL.GetDIMtotalTime(GUID);

            double totalSecond = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                totalSecond = totalSecond + double.Parse((dt.Rows[i]["DIM_REF_TIME"].ToString()));
            }

            double min = totalSecond / (double)60;

            return min;
        }

        public double GetRefTotalTimeMinute_xiaxian(string GUID)
        {
            if (string.IsNullOrEmpty(GUID))
            {
                return 0;
            }

            DataTable dt = RptDAL.GetDIMtotalTime_xiaxian(GUID);

            double totalSecond = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                totalSecond = totalSecond + double.Parse((dt.Rows[i]["DIM_REF_TIME_XIAXIAN"].ToString()));
            }

            double min = totalSecond / (double)60;

            return min;
        }

        public double GetRefTotalTimeMinute_shangxian(string GUID)
        {
            if (string.IsNullOrEmpty(GUID))
            {
                return 0;
            }

            DataTable dt = RptDAL.GetDIMtotalTime_shangxian(GUID);

            double totalSecond = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                totalSecond = totalSecond + double.Parse((dt.Rows[i]["DIM_REF_TIME_SHANGXIAN"].ToString()));
            }

            double min = totalSecond / (double)60;

            return min;
        }




        //心理健康等级-评语
        public string GetXljkLevelAndComments(string GUID)
        {
            if (string.IsNullOrEmpty(GUID)) { return string.Empty; }
            return RptDAL.GetXljkLevelAndComments(GUID);
        }

        public double GetSingleScore(int dimID, int originalScore)
        {
            double fRet = 0.0f;
            if (dimID < 0 || originalScore < 0)
            {
                return fRet;
            }
            else
            {
                Dimension dim = new Dimension();
                dim = DimensionDAL.GetModel(dimID);
                double[] dimP = {dim.Dimension_2,//1
                                dim.Dimension_8,//2
                                dim.Dimension_17,//3
                                dim.Dimension_32,//4
                                dim.Dimension_50,//5
                                dim.Dimension_68,//6
                                dim.Dimension_83,//7
                                dim.Dimension_92,//8
                                dim.Dimension_98};//9
                double[] dimScore = { 0.01, 0.03, 0.10, 0.28, 0.59, 0.76, 0.89, 0.98, 0.92 };

                //查找最接近的一个值
                Double searchValue = originalScore;
                Double currentNearest = dimP[0];
                int nearestPos = 0;                
                Double currentDifference = Math.Abs(currentNearest - searchValue);

                for (int i = 1; i < dimP.Length; i++)
                {
                    Double diff = Math.Abs(dimP[i] - searchValue);
                    if (diff < currentDifference)
                    {
                        currentDifference = diff;
                        currentNearest = dimP[i];
                        nearestPos = i;
                    }
                }

                fRet = dimScore[nearestPos];
                return fRet;
            }
        }

        public double ConvertDimScore(int dimID, double originalScore)
        {
            double fRet = 0.0f;
            if (dimID < 0 || originalScore < 0)
            {
                return fRet;
            }
            else
            {
                Dimension dim = new Dimension();

                dim = DimensionDAL.GetModel(dimID);

                double[] dimP = {dim.Dimension_2,//1
                                dim.Dimension_8,//2
                                dim.Dimension_17,//3
                                dim.Dimension_32,//4
                                dim.Dimension_50,//5
                                dim.Dimension_68,//6
                                dim.Dimension_83,//7
                                dim.Dimension_92,//8
                                dim.Dimension_98};//9

                if (originalScore <= dimP[0]) return 1.0f;
                if (originalScore >= dimP[8]) return 9.0f;

                double[,] dimscore = new double[8, 10];

                for (int r = 0; r < 8; r++)
                {
                    double diff = (dimP[r + 1] - dimP[r]) / 10;
                    for (int c = 0; c < 10; c++)
                    {
                        dimscore[r, c] = dimP[r] + (diff * c);
                        dimscore[r, c] = Math.Round(dimscore[r, c], 0, MidpointRounding.AwayFromZero);
                    }
                }

                for (int i = 0; i < 8; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        if (dimscore[i, j] >= originalScore)//返回右边值？
                        {
                            fRet = (i + 1) + (j * 0.1);
                            return fRet;
                        }
                    }
                }
                return fRet;
            }
        }
        
        public int ConvertZhiLiDimScore(int dimID, double originalScore)
        {
            int nRet = 0;
            if (dimID < 0 || originalScore < 0)
            {
                return nRet;
            }
            else
            {
                Dimension dim = new Dimension();

                dim = DimensionDAL.GetModel(dimID);

                double[] dimP = {dim.Dimension_2,//1
                                dim.Dimension_8,//2
                                dim.Dimension_17,//3
                                dim.Dimension_32,//4
                                dim.Dimension_50,//5
                                dim.Dimension_68,//6
                                dim.Dimension_83,//7
                                dim.Dimension_92,//8
                                dim.Dimension_98};//9

                if (originalScore <= dimP[0]) return 2;
                if (originalScore >= dimP[8]) return 98;

                double[,] dimscore = new double[8, 10];

                for (int r = 0; r < 8; r++)
                {
                    double diff = (dimP[r + 1] - dimP[r]) / 10;
                    for (int c = 0; c < 10; c++)
                    {
                        dimscore[r, c] = dimP[r] + (diff * c);
                        dimscore[r, c] = Math.Round(dimscore[r, c], 3, MidpointRounding.AwayFromZero);
                    }
                }

                double[] dimZhiLi = { 4.006, 10.565, 22.663, 40.129, 50.000, 59.871, 77.337, 89.435, 95.994 };

                for (int i = 0; i < 8; i++)
                {
                    double diff = (dimP[i + 1] - dimP[i]) / 10;
                    for (int j = 0; j < 10; j++)
                    {
                        if (dimscore[i, j] >= originalScore)//返回右边值？
                        {
                            nRet = (int)Math.Round(dimZhiLi[i] + (j * diff),0,MidpointRounding.AwayFromZero);
                            //return (int)Math.Round(()nRet, 0, MidpointRounding.AwayFromZero);
                            return nRet;
                        }
                    }
                }
                return nRet;
            }
        }

        public string GetDimScoreLevel(string dimID, int originalScore)
        {
            if (string.IsNullOrEmpty(dimID) || originalScore < 0)
            {
                return string.Empty;
            }
            return string.Empty;
        }
    }
}
